<style scoped>
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .weui-loadmore_line .weui-loadmore__tips {
    background-color: #eee;
  }
  .rj-cell.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell label {
    color: #333;
    font-size: 14px;
    width: 44%;
    display: inline-block;
    padding: 5px 3%;
    text-align: right;
  }
  .rj-cell span {
    font-size: 14px;
    color: #333;
    display: inline-block;
    width: 50%;
  }
  .rj-cell.note label{
    float: left;
  }
  .rj-cell.note span{
    width: 80%;
    display: inline-block;
  }
  .weui-cells {
    margin-top: 0;
  }
  .weui-cells_form {
    background: #fbfbfb;
  }
  .weui-cell {
    background: #fff;
  }
  .weui-cells__title {
    text-align: left;
  }
  .oitem {
    padding: 3px;
  }
  .hide {
    display: none;
  }
  #acts {
    font-size: 0;
  }
  #acts span {
    font-size: 14px;
  }
  .halfbtn {
    display: inline-block;
    width: 49%;
  }
  .weui-label {
    text-align: left;
  }
  .time {
    border: none;
    font-size: 17px;
  }
  .weui-cell_select {
    padding: 10px 15px;
  }
  .weui-select {
    height: auto;
    line-height: normal;
  }


  .weui_uploader_input_wrp {
    float: left;
    position: relative;
    margin-right: 9px;
    margin-bottom: 9px;
    width: 77px;
    height: 77px;
    border: 1px solid #D9D9D9;
  }
  .weui_uploader_hd {
    padding-top: 0;
    padding-right: 0;
    padding-left: 0;
  }
  .weui_uploader_files {
    list-style: none;
  }
  .weui_uploader_input_wrp:before {
    width: 2px;
    height: 39.5px;
  }
  .weui_uploader_input_wrp:before, .weui_uploader_input_wrp:after {
    content: " ";
    position: absolute;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    background-color: #D9D9D9;
  }
  .weui_uploader_input {
    position: absolute;
    z-index: 1;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  }
  .weui_uploader_input_wrp:after {
    width: 39.5px;
    height: 2px;
  }
  .weui_uploader_bd {
    overflow: hidden;
  }
  .weui_cell {
    padding: 10px 15px;
    position: relative;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
  }
  .outstore >>> .weui-uploader_files {
    list-style: none;
    overflow-y: hidden;
  }
  .outstore >>> .weui-uploader_file {
    float: left;
    margin-right: 9px;
    margin-bottom: 9px;
    width: 79px;
    background: no-repeat 50%;
    background-size: cover;
  }
  .weui-cell.fir {
    border-top: 0;
  }
  .help {
    text-align: right;
    padding: 5px;
    font-size: 12px;
    color: #bbb;
  }
  .weui_uploader_input_wrp.inrow {
    width: 30px;
    height: 30px;
    margin: 0;
  }
  .weui_uploader_input_wrp:before {
    height: 19.5px;
  }
  .weui_uploader_input_wrp:after {
    width: 19.5px;
  }
  .weui-uploader_file.inrow {
    margin: 0;
    width: 40px;
  }
  li.weui-uploader_file {
    display: inline-block;
    padding: 3px 5px;
  }
  ul#images {
    text-align: right;
  }

  .addsell >>> .weui-toast--text {
    width: 80% !important;
    text-align: left !important;
    font-size: 16px !important;
  }
  .addsell >>> p.weui-toast_content {
    text-align: left !important;
  }
</style>
<template>
  <div class="addsell">
    <div style="height: 50px; line-height: 50px; background: #f7f7f7; text-align: center; font-size: 16px; border-bottom: 1px solid #bdbbbc; position: relative">
      编辑客户
      <a href="javascript:;" @click="$router.back(-1)" style="position: absolute; left: 10px; top: 8px">
        <img src="@/assets/back.png" style="width: 20px" />
      </a>
    </div>
    <div class="weui-cells weui-cells_form">
      <form method="post" id="formid">
        <input type="hidden" name="guid" :value="id" />
        <input type="hidden" name="type" :value="customer.type" />
        <div class="cellgroup">
          <div class="weui-cell weui-cell-select">
            <div class="weui-cell__hd"><label class="weui-label">客户类型</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="flag" v-model="ctype">
                <option selected="" v-for="(item, index) in cusTypes" :value="item.dictkey">{{item.dicttext}}</option>
              </select>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">名称</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" :readonly="customer.status == 1" type="text" name="name" :value="customer.name" lay-verify="required" />
            </div>
          </div>
<!--          <div class="weui-cell">-->
<!--            <div class="weui-cell__hd"><label class="weui-label">法人</label></div>-->
<!--            <div class="weui-cell__bd">-->
<!--              <input class="weui-input" type="text" name="legal" :value="customer.legal" />-->
<!--            </div>-->
<!--          </div>-->
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">商务人员</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" name="manager" :value="customer.manager" />
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">商务电话</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" name="managertel" :value="customer.managertel" />
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">商务邮箱</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" name="memail" :value="customer.memail" />
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">财务人员</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" name="financer" :value="customer.financer" />
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">财务电话</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" name="fintel" :value="customer.fintel" />
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">财务邮箱</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" name="finemail" :value="customer.finemail" />
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">注册资金</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" style="width: 55%;display: inline-block;" type="number" name="registcapital" :value="customer.registcapital" />
              <input class="weui-input" style="width: 40%;display: inline-block;" type="number" value="万元" />
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">收货人</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" name="contact" :value="customer.contact" />
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">联系电话</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="number" name="contactno" v-model.number="customer.contactno" />
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd">
              <label class="weui-label">所在地区</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" id='province' name="province"></div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">详细地址</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" :value="customer.address" name="address" lay-verify="required" />
            </div>
          </div>
          <div class="weui-cell weui-cell_select">
            <div class="weui-cell__hd"><label class="weui-label">结算币种</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="currency" v-model="currency" >
                <option value="" disabled>请选择</option>
                <option selected="" v-for="(item, index) in currencys" :key="item.dictkey" :value="item.dictkey">{{item.dicttext}}</option>
              </select>
            </div>
          </div>
        </div>

        <div class="weui-btn-area">
          <!--          <a class="weui-btn weui-btn_primary halfbtn" href="javascript:" @click="postForm(1)">保存</a>-->
          <a class="weui-btn weui-btn_primary subbtn" href="javascript:" @click="postForm(2)">提交</a>
        </div>
      </form>
    </div>

  </div>
</template>

<script>

  export default {
    name: 'EditCustomer',
    data () {
      return {
        id: '',
        customer: '',
        openid: localStorage.openid,//'oIuV2v77MaLCAQILhjI6h6oTWpHI',
        username: localStorage.username,
        currencys: [], //结算币种
        currency: '',
        cusTypes: '',
        ctype: ''
      }
    },
    computed: {
      hasCustomer: function () {
        return this.customerid != '' ? true : false;
      }
    },
    created() {
      window.addEventListener('setItem', ()=> {
        this.openid = localStorage.getItem('openid');
        this.username = localStorage.getItem('username');
      })
    },
    mounted () {
      let that = this;

      //地址初始化
      $("#province").cityPicker();

      this.id = this.$route.query.id;
      this.getBaseInfo(this.id);
      this.getCurrencys();
      this.getCusTypes();
    },
    watch: {

    },
    methods: {
      getBaseInfo: function(id) {
        this.axios({
          method: 'post',
          url: '/sales/Customer!detail.action',
          params: {customerid: id}
        }).then(response => {
          if (response.data.msg == 'success') {
            this.customer = response.data.customer == null ? {} : response.data.customer;
            this.currency = this.customer.currency;
            this.ctype = this.customer.flag;
            $('#province').val( this.customer.province + ' ' + this.customer.city + ' ' + this.customer.district);
            // $('#type').val(this.customer.type);
          } else if (response != undefined && response.data != undefined && response.data.msg == 'sessionTimeOut') {
          //不不设置提示
        } else {
          this.toastError('系统错误');
        }
        });
      },
      getCurrencys: function() {
        let that = this;
        this.axios({
          method: 'get',
          responseType: 'json',
          url: '/sales/Sellform!findDict.action',
          params: {type: 92}
        }).then(response => {
          if (response.statusText == "OK") {
            that.currencys = response.data;
          } else {
            this.toastText('获取结算币种数据失败');
          }
        });
      },
      getCusTypes: function() {
        let that = this;
        this.axios({
          method: 'get',
          responseType: 'json',
          url: '/sales/Sellform!findDict.action',
          params: {type: 119}
        }).then(response => {
          if (response.statusText == "OK") {
            that.cusTypes = response.data;
          } else {
            this.toastText('获取运费计价方式数据失败');
          }
        });
      },
      //提交表单数据
      postForm: function(type) {
        let that = this;
        if ($('input[name="name"]').val() == '') {
          this.toastText('请输入客户名称！');
          return false;
        } else if ($('input[name="province"]').val().length > 50) {
          this.toastText('客户名称太长，不符合系统要求！');
          return false;
        } else if ($('input[name="province"]').val() == '') {
          this.toastText('请选择收货城市！');
          return false;
        }
        // else if ($('input[name="province"]').val().length > 20) {
        //   this.toastText('收货城市太长，不符合系统要求！');
        //   return false;
        // }
        else if ($('input[name="address"]').val() == '') {
          this.toastText('请输入详细地址！');
          return false;
        } else if ($('input[name="address"]').val().length > 200) {
          this.toastText('详细地址太长，不符合系统要求！');
          return false;
        } else if ($('input[name="contact"]').val() == '') {
          this.toastText('请输入收货联系人！');
          return false;
        } else if ($('input[name="contact"]').val().length > 30) {
          this.toastText('收货联系人太长，不符合系统要求！');
          return false;
        } else if ($('input[name="contactno"]').val() == '') {
          this.toastText('请输入收货联系人电话！');
          return false;
        } else if ($('input[name="contactno"]').val().length > 25) {
          this.toastText('联系人电话太长，不符合系统要求！');
          return false;
        }

        //防止重复提交
        $('.subbtn').attr("disabled",true).css("pointer-events","none");
        //显示提交状态
        $.showLoading("正在提交");

        let postData = this.serialize2Obj($("#formid").serializeArray());

        this.axios({
          method: 'post',
          url: '/sales/Customer!update.action',
          params: postData
        }).then(response => {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");
          //关闭提交状态
          $.hideLoading();

          let res = response.data;
          if (res.msg == "success") {
            that.toastSuccess("修改成功");
            that.$router.push({name: 'CustomerList'});
          }  else{
            that.toastError("系统错误");
          }
        }).catch(function (error) {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");
          //关闭提交状态
          $.hideLoading();
          that.toastError('系统错误');
        });
      }
    }
  }
</script>
